using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Reflection;

namespace 泛解析去重系统
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Control.CheckForIllegalCrossThreadCalls = false;
            //利用双缓冲技术优化datagridview加载过多导致界面卡顿的问题
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer //双缓冲
                | ControlStyles.ResizeRedraw
               | ControlStyles.AllPaintingInWmPaint, //不擦除背景，减少闪烁
                true);
            this.UpdateStyles();
            //利用反射设置DataGridView的双缓冲
            Type dgvType = this.dataGridView1.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
               BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(this.dataGridView1, true, null);

        }
        public Thread dom;
        public Thread doms;
        private void button2_Click(object sender, EventArgs e)
        {
            toolStripStatusLabel2.Text = "执行中";
            dataGridView1.Rows.Clear();
            button2.Enabled = false;//禁止
            button3.Enabled = true;//启用
            dom = new Thread(HttpGetRequest);
            dom.Start();
        }
        /// <summary>
        /// 非ssl请求
        /// </summary>
        public void HttpGetRequest()
        {
            string reqtype = "http://";
            string url = Urls.ToString();
            string[] s1 = url.Split('\r','\n');
            foreach(string urlcode in s1)
            {
                string nocode = urlcode;//拼接为一个字符串
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(reqtype + nocode);
                    request.Method = "GET";
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream str = response.GetResponseStream();
                        StreamReader reader = new StreamReader(str);
                        string code = reader.ReadToEnd();
                        if (code.Length > 100)
                        {
                            dataGridView1.Rows.Add(reqtype + nocode, code.Length, "疑似真实",DateTime.Now.ToString());
                        }
                        else
                        {
                            Console.WriteLine("长度小于100，跳过解析");
                        }
                    }
                    else
                    {
                        Console.WriteLine("网站无法访问");
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            //创建线程来执行http
            doms = new Thread(HttpsGetRequest);
            doms.Start();
        }
        /// <summary>
        /// ssl请求
        /// </summary>
        public void HttpsGetRequest()
        {
            string reqtype = "https://";
            string url = Urls.ToString();
            string[] s1 = url.Split('\r', '\n');
            foreach (string urlcode in s1)
            {
                string nocode = urlcode;//拼接为一个字符串
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(reqtype + nocode);
                    request.Method = "GET";
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream str = response.GetResponseStream();
                        StreamReader reader = new StreamReader(str);
                        string code = reader.ReadToEnd();
                        if(code.Length > 100)
                        {
                            dataGridView1.Rows.Add(reqtype + nocode, code.Length, "疑似真实", DateTime.Now.ToString());
                        }
                        else
                        {
                            Console.WriteLine("长度小于100，跳过解析");
                        }
                    }
                    else
                    {
                        Console.WriteLine("网站无法访问");
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            button2.Enabled = true;
            button3.Enabled = false;
            toolStripStatusLabel2.Text = "线程终止";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //终止线程
            if(dom != null && dom.IsAlive)
            {
                button2.Enabled = true;
                button3.Enabled = false;//禁止
                toolStripStatusLabel2.Text = "已终止";
                dom.Abort();
            }
            if(doms != null && doms.IsAlive)
            {
                button2.Enabled = true;
                button3.Enabled = false;//禁止
                toolStripStatusLabel2.Text = "已终止";
                doms.Abort();
            }
        }
        public static StringBuilder Urls;
        private void button1_Click(object sender, EventArgs e)
        {
            Urls = new StringBuilder();
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "文本文件 (*.txt)|*.txt";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;
                textBox1.Text = filePath;

                try
                {
                    using (StreamReader reader = new StreamReader(filePath))
                    {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            // 按行打开并在控制台输出
                            Urls.Append(line + "\r\n");
                        }
                        button2.Enabled = true;
                    }
                }
                catch (IOException ex)
                {
                    MessageBox.Show("文件打开错误: " + ex.Message);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button3.Enabled = false;//禁用
        }
    }
}
